package com.cmy.framework_widget.wheelview

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF

class WheelItem(// 起点Y坐标、宽度、高度
      start_y: Float, private val width: Int, private val height: Int, //字体大小、颜色
    private val fontColor: Int, private val fontSize: Int, var text: String
) {

   var startY: Float = start_y
      set(value) {
        field = value
        rect.left = 0f
        rect.top = startY
        rect.right = width.toFloat()
        rect.bottom = startY + height
    }

    //四点坐标
    private val rect = RectF()
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        adjust(0f)
    }

    /**
     * 根据Y坐标的变化值，调整四点坐标值
     * @param dy
     */
    fun adjust(dy: Float) {
        startY += dy
        rect.left = 0f
        rect.top = startY
        rect.right = width.toFloat()
        rect.bottom = startY + height
    }


    /**
     * 直接设置Y坐标属性，调整四点坐标属性
     * @param startY
     */
//    fun setStartY(startY: Float) {
//        this.startY = startY
//        rect.left = 0f
//        rect.top = startY
//        rect.right = width.toFloat()
//        rect.bottom = startY + height
//    }

    fun onDraw(mCanvas: Canvas) {
        //设置钢笔属性
        mPaint.textSize = fontSize.toFloat()
        mPaint.color = fontColor
        //得到字体的宽度
        val textWidth = mPaint.measureText(text).toInt()
        //drawText的绘制起点是左下角,y轴起点为baseLine
        val metrics = mPaint.fontMetrics
        val baseLine =
            (rect.centerY() + (metrics.bottom - metrics.top) / 2 - metrics.bottom).toInt()
        //居中绘制
        mCanvas.drawText(text, rect.centerX() - textWidth / 2, baseLine.toFloat(), mPaint)
    }
}